package com.tenton.service.Impl;

import com.tenton.mapper.BookMapper;
import com.tenton.pojo.Book;
import com.tenton.service.BookService;
import com.tenton.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Date: 2021/1/18
 * @Author: Tenton
 * @Description:
 */
public class BookServiceImpl implements BookService {
    /**
     * 新增图书
     * @Author: Tenton
     * @param book
     * @return
     */
    @Override
    public int insertBook(Book book) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        int result = mapper.insertBook(book);
        return result;
    }

    /**
     * 删除图书
     * @Author: Tenton
     * @param id
     * @return
     */
    @Override
    public int deleteBook(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        int result = mapper.deleteBook(id);
        return result;
    }

    /**
     * 修改图书
     * @Author: Tenton
     * @param book
     * @return
     */
    @Override
    public boolean updateBook(Book book) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        boolean flag = mapper.updateBook(book);
        return flag;
    }

    /**
     * 根据图书Id查询图书
     * @Author: Tenton
     * @param id
     * @return
     */
    @Override
    public Book getBook(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        Book book = mapper.getBook(id);
        return book;
    }

    /**
     * 获取所有图书
     * @Author: Tenton
     * @return
     */
    @Override
    public List<Book> listBook() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Book> books = mapper.listBook();
        return books;
    }
}
